<?php

namespace App\Models;

class Seller extends BaseModel
{
  protected $casts = [
    'perms' => 'array'
  ];

  public function scopeWhenSellerName($q, $name)
  {
    $q->when($name, function ($q) use ($name) {
      $q->whereHas('user', function ($q) use ($name) {
        $q->where('username', $name);
      });
    });
  }

  public function father()
  {
    return $this->belongsTo(self::class, 'pid', 'id')->with('user:id,role,username,avatar');
  }

  public function subs()
  {
    return $this->hasMany(self::class, 'pid', 'id')->with('user:id,role,username,avatar');
  }

  public function user()
  {
    return $this->belongsTo(User::class);
  }

  public function oprationLogs()
  {
    return $this->morphMany(OprationLog::class, 'logable');
  }

  public function calcCoins($top_up)
  {
    $fee = bcmul(abs($top_up), $this->fee_ratio, 3);
    $coins = bcsub(bcadd($this->coins, $top_up), $fee);
    if ($coins < 0) {
      fail('币数不足');
    }
    $this->coins = $coins;
  }

  public function buys()
  {
    return $this->hasMany(Sell::class);
  }

  public function buysToday()
  {
    return $this->buys()->whereDate('created_at', today());
  }

  public function boughts()
  {
    return $this->buys()->where('state', 'done');
  }

  public function boughtsToday()
  {
    return $this->boughts()->whereDate('unfreeze_at', today());
  }

  public function sells()
  {
    return $this->hasMany(Buy::class);
  }

  public function sellsToday()
  {
    return $this->sells()->whereDate('created_at', today());
  }

  public function waitingSells()
  {
    return $this->sells()->where('state', 'waiting');
  }

  public function waitingSellsToday()
  {
    return $this->waitingSells()->whereDate('created_at', today());
  }

  public function solds()
  {
    return $this->sells()->where('state', 'done');
  }

  public function soldsToday()
  {
    return $this->solds()->whereDate('unfreeze_at', today());
  }
  // 被用户取消的
  public function canceled()
  {
    return $this->hasMany(Cancel::class);
  }

  public function canceledToday()
  {
    return $this->canceled()->whereDate('created_at', today());
  }

  public function createLog($inputs)
  {
    if ($inputs['password'] != '') {
      $this->oprationLogs()->create([
        'oprator_user_id' => auth()->id(),
        'content' => "修改商户 {$this->user->username} 密码"
      ]);
    }
    $changes = $this->getChanges();
    if (isset($changes['coins'])) {
      // $action = $inputs['top_up'] > 0 ? '加' : '减';
      $ratio = bcmul($this->fee_ratio, 1000, 2);
      $fee = bcmul(abs($inputs['top_up']), $this->fee_ratio, 3);
      $withFee = bcsub($inputs['top_up'], $fee);

      $this->oprationLogs()->create([
        'oprator_user_id' => auth()->id(),
        'content' => "给商户 {$this->user->username} 人工 {$inputs['top_up']} FCB，手续费 ({$ratio}‰) {$fee} FCB；实际到可动 {$withFee}"
      ]);
    }
    if (isset($changes['fee_ratio'])) {
      $this->oprationLogs()->create([
        'oprator_user_id' => auth()->id(),
        'content' => "修改商户 {$this->user->username} 买币手续费为 {$this->fee_ratio}"
      ]);
    }
    if (isset($changes['enable'])) {
      $act = $this->enable ? '启用' : '禁用';
      $this->oprationLogs()->create([
        'oprator_user_id' => auth()->id(),
        'content' => "{$act}商户 {$this->user->username}"
      ]);
    }
    if (isset($changes['tip'])) {
      $this->oprationLogs()->create([
        'oprator_user_id' => auth()->id(),
        'content' => "修改商户 {$this->user->username} 下发手续费为 {$this->tip}"
      ]);
    }
  }
}
